package com.example.Service.impl;

import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysRoleMenu;
import com.atguigu.vo.system.AssginMenuVo;
import com.atguigu.vo.system.MetaVo;
import com.atguigu.vo.system.RouterVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.Mapper.SysMenuMapper;
import com.example.Mapper.SysRoleMapper;
import com.example.Mapper.SysRoleMenuMapper;
import com.example.OmegaException;
import com.example.Service.SysMenuService;
import com.example.Service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;

    public List<SysMenu> getlist() {
        List<SysMenu> list = sysMenuMapper.selectList(null);
        if (CollectionUtils.isEmpty(list)) return null;
        return containTree(list, 0L);

    }

    private List<SysMenu> containTree(List<SysMenu> list, Long pid) {
        List<SysMenu> list1 = new ArrayList<>();
        for (SysMenu sysMenu : list) {
            if (sysMenu.getParentId().equals(pid)) {
                sysMenu.setChildren(containTree(list, sysMenu.getId()));
                list1.add(sysMenu);
            }
        }
        return list1;
    }

    public void delete(Integer id) {
        LambdaQueryWrapper<SysMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysMenu::getParentId, id);
        List<SysMenu> list = sysMenuMapper.selectList(lambdaQueryWrapper);
        if (list.size() > 0) {
            throw new OmegaException(600, "该菜单下有子菜单，不可删除");
        }
        sysMenuMapper.deleteById(id);

    }

    public List<SysMenu> getlistByRoleId(Integer roleId) {
        List<SysMenu> alllist = this.list();
        LambdaQueryWrapper<SysRoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenu::getRoleId, roleId);
        lambdaQueryWrapper.select(SysRoleMenu::getMenuId);
        List<SysRoleMenu> list = sysRoleMenuMapper.selectList(lambdaQueryWrapper);
        List<Long> list1 = list.stream().map(c -> c.getMenuId()).collect(Collectors.toList());
        List<SysMenu> list2 = new ArrayList<>();

        for (SysMenu sysMenu : alllist) {
            if (list1.contains(sysMenu.getId())) {
                list2.add(sysMenu);
            }
        }
        return containTree(list2, 0L);


    }

    public void doassign(AssginMenuVo assiginMenuVo) {
        LambdaQueryWrapper<SysRoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenu::getRoleId, assiginMenuVo.getRoleId());
        sysRoleMenuMapper.delete(lambdaQueryWrapper);
        if (assiginMenuVo.getMenuIdList().size() == 0) return;
        for (Long menuid : assiginMenuVo.getMenuIdList()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(assiginMenuVo.getRoleId());
            sysRoleMenu.setMenuId(menuid);
            sysRoleMenuMapper.insert(sysRoleMenu);
        }

    }

    public List<RouterVo> getUserMenulist(Long userId) {
        List<SysMenu> list=null;
        if(userId.longValue() == 1){
            LambdaQueryWrapper<SysMenu> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SysMenu::getStatus,1).orderByAsc(SysMenu::getSortValue);
            list=this.list(lambdaQueryWrapper);


        }else{
            list=sysMenuMapper.findByUserId(userId);
        }
        List<SysMenu> list1=containTree(list,0L);
        List<RouterVo> routerVoList=buildrouterMenu(list1);
        return routerVoList;

    }

    private List<RouterVo> buildrouterMenu(List<SysMenu> list1) {
        List<RouterVo> routers=new LinkedList<RouterVo>();
        for (SysMenu sysMenu:list1){
            RouterVo router=new RouterVo();
            router.setHidden(false);
            router.setAlwaysShow(false);
            router.setComponent(sysMenu.getComponent());
            router.setPath(getRouterpath(sysMenu));
            router.setMeta(new MetaVo(sysMenu.getName(),sysMenu.getIcon()));
            List<SysMenu> children=sysMenu.getChildren();
            if(sysMenu.getType().intValue()==1){
                List<SysMenu> hiddenMenuList=children.stream().filter(item->!StringUtils.isEmpty(item.getComponent())).collect(Collectors.toList());
                for (SysMenu hiddenMenu:hiddenMenuList){
                    RouterVo hiddenRouter=new RouterVo();
                    hiddenRouter.setHidden(true);
                    hiddenRouter.setAlwaysShow(false);
                    hiddenRouter.setPath(getRouterpath(hiddenMenu));
                    hiddenRouter.setComponent(hiddenMenu.getComponent());
                    hiddenRouter.setMeta(new MetaVo(hiddenMenu.getName(),hiddenMenu.getIcon()));
               routers.add(hiddenRouter);
                }
            }else{
                if(!CollectionUtils.isEmpty(children)){
                    if(children.size()>0){
                        router.setAlwaysShow(true);
                    }
                    router.setChildren(buildrouterMenu(children));
                }
            }
            routers.add(router);

        }
        return routers;
    }

    private String getRouterpath(SysMenu sysMenu) {
        String routerPath="/"+sysMenu.getPath();
        if(sysMenu.getParentId().intValue()!=0){
            routerPath=sysMenu.getPath();
        }
        return routerPath;
    }
    public List<String> getbuttonList(Long userId){
        List<SysMenu> list=null;
        if(userId.longValue() == 1){
            list=this.list();

        }else{
            list=sysMenuMapper.findByUserId(userId);
        }
        List<String> list1=list.stream().filter(item->item.getType() == 2).map(item->item.getPerms()).collect(Collectors.toList());
        return list1;

    }

}
